package com.droidnova.android.games.vortex;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Layout;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class Vortex extends Activity implements ColorPickerDialog.OnColorChangedListener{
	//color variables
	private static final int DIALOG_TEXT_ENTRY = 7;
	private static final int DIALOG_FILE_ENTRY = 8;
	private static final int DIALOG_FILE_ENTRY1 = 9;  
	private static final int DIALOG_SLICE_ENTRY = 6;
	private String[] mFileList;
	private File mPath = new File(Environment.getExternalStorageDirectory() + "/");
	private String mChosenFile;
	private static final String FTYPE = ".txt"; 
	private static final String BRIGHTNESS_PREFERENCE_KEY = "brightness";
	private static final String COLOR_PREFERENCE_KEY = "color";
	int colorval=0;
	int colorval1=0;
	//TableLayout.LayoutParams rowInTableLp;
	TableRow.LayoutParams rowInTableLp;
	//slice reset
	int slice_flag=0;
	
	//range to calculate earray text
	float range=0.0f;
	InputMethodManager imm;

	//static int a = 2;
	private export export1;
	private VortexRenderer _VortexRenderer;
	private lines linecube1;
	float _pcolors[] ,_pvertices[],_pintensity[];
	points1 pointcube1;

	int _noVertices;
	//Intent i;
	Intent myIntent;
	EditText text;

	int no;
	int r =20;

	//dynamic table variables
	TableLayout tl,t3;
	TableRow rowtext,rowtext1,rowtext2,first;
	int _levels=4;
	TextView barray[],init_barray[];
	CheckBox ckarray[], init_ckarray[],ckarrayzero;
	EditText earray[], init_earray[];
	TextView earrayzero,barrayzero;
	TableRow.LayoutParams params ,params1 ,params2;
	
	//reset
	//Copy arrays for reset
	float[] copy_c,copy_v ,spcolor,copy_2v;
	short[] copy_i;
	int copy_noVertices ,copy_2noVertices;
	float copy_xmax,copy_ymax,copy_zmax ,copy_max, copy_xmin, copy_ymin, copy_zmin;
	//slice
	float dec_value=0.0f;

	//initial table
	int init_no= 5;
	
	//alert dialog
	int which1=0;
	
	

	EditText ed1;
	
	//dialog slice
	CheckBox ck4;
	String x;
	
	//camera flag
	int camera_flag=0;
	int shade_flag=0;
	
	//variables for surface
    //file load flag
	int load=0;
	float aa=0.0f;
	
	short[] _pindicesnew2 =null;
	//texviews
	
	TextView button4,button1,button,tb,shade,button2,slice1,slice2,Nocolor,mintext,maxtext,sur;
	LinearLayout layout;
	
	//cube
	 private cube1 cube1;
	 
	 int surf=0;
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		//NUMPER PICKER DIALOG
		case DIALOG_TEXT_ENTRY:
			LayoutInflater factory = LayoutInflater.from(this);
			//final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry1, null);
			final View textEntryView = factory.inflate(R.layout.main1, null);
			return new AlertDialog.Builder(this)
			.setTitle("Set Number")
			.setView(textEntryView)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					NumberPicker np = (NumberPicker) textEntryView.findViewById(R.id.numpic);
					_levels=np.getCurrent();
					Log.v("getvaluetext","getvaluetext->"+_levels);
					createB(_levels);
//					createSurface(_levels);
					/* User clicked OK so do some stuff */
				}
			})
			.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {

					/* User clicked Cancel so do some stuff */
				}
			})
			.create();
			//EXIT DIALOG
		case DIALOG_FILE_ENTRY:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("You want to exit. Are you sure ?")
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Vortex.this.finish();
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alert = builder.create();
			return alert;
			//FILE PICKER DIALOG
		case DIALOG_FILE_ENTRY1:
			Dialog dialog = null;
			
			AlertDialog.Builder builder1 = new Builder(this);

			builder1.setTitle("Click a file to load");
			if(mFileList == null){
				Log.e("log1", "Showing file picker before loading the file list");

			}
			builder1.setPositiveButton("Open", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					try
					{
					mChosenFile = mFileList[which1];
					//first time of file fload
					surf=0;
					if(load==0)
					{
						//export instance
						export1 = new export(getBaseContext(),mChosenFile);
						cube1 = new cube1();
						_pcolors = export1._pcolors;
						_pvertices = export1._pvertices;
						_noVertices = export1._noVertices;
						_pintensity = export1._intensity;
						//pointcube instance
						pointcube1 = new points1(export1._pcolors, export1._pvertices,export1._noVertices,export1._pindicesnew);
						
						// initial copy array for surf;
						spcolor = (float[])export1._pcolors.clone();
						for(i=0;i<(spcolor.length/4);i++)
						{
							spcolor[(4*i)+3]=0.2f;
						}
						//initial copy array for reset
						copy_c = (float[])export1._pcolors.clone();
				        copy_v =(float[])export1._pvertices.clone();
				        copy_i = export1._pindicesnew.clone();
				        copy_noVertices = export1._noVertices;
				        //settings for slice
				        copy_xmax = export1.getMaxXValue(copy_v);
				        copy_ymax = export1.getMaxYValue(copy_v);
				        copy_zmax = export1.getMaxZValue(copy_v);
				        copy_xmin = export1.getMinXValue(copy_v);
				        copy_ymin = export1.getMinYValue(copy_v);
				        copy_zmin = export1.getMinZValue(copy_v);
				        //line cube
				        linecube1 = new lines(copy_xmax, copy_ymax, copy_zmax, copy_xmin, copy_ymin, copy_zmin);
				        //Renderer
				        _VortexRenderer = new VortexRenderer(getBaseContext() , pointcube1,linecube1,camera_flag,cube1,surf,export1.incx,export1.incy,export1.incz,export1._pcolors,export1.l1,export1.l2,export1.l3);
					    //pixel format
				        _VortexRenderer.getHolder().setFormat(PixelFormat.RGBA_8888);
				        //Content View
				        setContentView(_VortexRenderer);
				        //Set Mintext and Maxtext
						mintext.setText("Min : "+String.valueOf(export1.min_value+"   "));
						maxtext.setText("Max : "+String.valueOf(export1.max_value));
						//Set Range
						range = export1.max_value -export1.min_value;
						//CreateB
						createB(_levels);
						//Set Visibility
						sur.setEnabled(true);
						t2.setVisibility(TextView.VISIBLE);
						tl.setVisibility(TableLayout.VISIBLE);
						//Add Content View
						addContentView(layout,
								new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
						
					}
					//subsequent file loads
					else
					{
						spcolor = null;
						export1.createVC(mChosenFile);
						//set pointcube
						//pointcube1.setPvertices_length(export1._pvertices.length);
						//pointcube1.setPcolors_length(export1._pcolors.length);
						//pointcube1.setPindices_length(export1._pindicesnew.length);
						pointcube1.set_pcolors(export1._pcolors);
						pointcube1.set_pvertices(export1._pvertices);
						pointcube1.set_noVertices(export1._noVertices);
						pointcube1.set_pindices(export1._pindicesnew);
						
						//setline cube
	                    linecube1.setA1(export1.getMaxXValue(export1._pvertices)) ;
	                    linecube1.setB1(export1.getMaxYValue(export1._pvertices)) ;
	                    linecube1.setC1(export1.getMaxZValue(export1._pvertices)) ;
	                    linecube1.set_pvertices1(linecube1.setvertex(linecube1.getA1(), linecube1.getB1(), linecube1.getC1(),linecube1.getAmin(),linecube1.getBmin(),linecube1.getCmin()));
	                    //display changed point cube and line cube
	                    _VortexRenderer.setSurf_flag(surf);
	                    // initial copy array for surf;
	                    
						spcolor = (float[])export1._pcolors.clone();
						for(i=0;i<(spcolor.length/4);i++)
						{
							spcolor[(4*i)+3]=0.2f;
						}
	                    //reset surface settings
	    				_VortexRenderer.setL1(export1.l1);
	    				_VortexRenderer.setL1(export1.l2);
	    				_VortexRenderer.setL1(export1.l3);
	    				_VortexRenderer.setIncx(export1.incx);
	    				_VortexRenderer.setIncy(export1.incy);
	    				_VortexRenderer.setIncz(export1.incz);
	    				
						_VortexRenderer.setPointcube(pointcube1);
						_VortexRenderer.setlinecube(linecube1);
						//Remove previous Views
						rowtext.removeAllViews();
						rowtext1.removeAllViews();
						rowtext2.removeAllViews();
			            //settings for mintext and maxtext
			            mintext.setText("Min : "+String.valueOf(export1.min_value+"   "));
			            maxtext.setText("Max : "+String.valueOf(export1.max_value));
			            shade.setText("Shade");
			            //Settings for Range
			            range = export1.max_value -export1.min_value;
			            //CreateB
			            createB(_levels); //was createB(4);
			        	//Copy arrays for reset
						copy_c = (float[])export1._pcolors.clone();
			            copy_v =(float[])export1._pvertices.clone();
			            copy_i = export1._pindicesnew.clone();
			            copy_noVertices = export1._noVertices;
			            //settings for slice
			            copy_xmax = export1.getMaxXValue(copy_v);
			            copy_ymax = export1.getMaxYValue(copy_v);
			            copy_zmax = export1.getMaxZValue(copy_v);
			            copy_xmin = export1.getMinXValue(copy_v);
				        copy_ymin = export1.getMinYValue(copy_v);
				        copy_zmin = export1.getMinZValue(copy_v);
			            slice_flag=0;
			           shade_flag=0;
			           
						
					}
					load++;
					
					}
					catch(Exception e)
					{
						e.printStackTrace();
						Toast.makeText(Vortex.this, " Application could not load specified file. Check file format and try again.", Toast.LENGTH_SHORT).show();
					}
				}
			});
			builder1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			
			builder1.setSingleChoiceItems(mFileList, -1, new DialogInterface.OnClickListener() {
		        public void onClick(DialogInterface dialog, int whichButton) {
		        	which1=whichButton; 

		        }
		    });

			dialog = builder1.create();
			return dialog;
		case DIALOG_SLICE_ENTRY:
			LayoutInflater sfactory = LayoutInflater.from(this);
			final View sView = sfactory.inflate(R.layout.slicelayout, null);
			return new AlertDialog.Builder(this)
			.setTitle("Slice Settings")
			.setView(sView)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					CheckBox ck1 = (CheckBox)sView.findViewById(R.id.checkBox1);
					CheckBox ck2 = (CheckBox)sView.findViewById(R.id.checkBox2);
					CheckBox ck3 = (CheckBox)sView.findViewById(R.id.checkBox3);
					ck4 = (CheckBox)sView.findViewById(R.id.checkBox4);
					ed1 = (EditText)sView.findViewById(R.id.editText1);
					
					//set slice x,y,z
					if(ck1.isChecked())
					{
						slice2.setEnabled(true);
						slice2.setClickable(true);
						slice2.setText("CutX");
					}
					else if(ck2.isChecked())
					{
						slice2.setEnabled(true);
						slice2.setClickable(true);
						slice2.setText("CutY");
					}
					else if(ck3.isChecked())
					{
						slice2.setEnabled(true);
						slice2.setClickable(true);
						slice2.setText("CutZ");
					}
					else
					{
						slice2.setEnabled(false);
						slice2.setClickable(false);
						slice2.setText("");
					}
					

				}
			})
			.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {

				}
			})
			.create();

		}

		return null;
	}

	private Context context;
	int[] bcolor = new int[11];
	LinearLayout t2;
	
	//ON CREATE METHOD
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		

		setContentView(R.layout.main);
		
		
		
		
		//initial array values and colors
		//final float init_values[] ={0f*(export1.max_value),.25f*(export1.max_value), .50f*(export1.max_value), .75f*(export1.max_value),1.0f*(export1.max_value)};
		//init_barray = new TextView[init_no];
		//init_earray = new EditText[init_no];
		//init_ckarray = new CheckBox[init_no];
		/*final int init_colors[]=  new int[5];
		init_colors[0]= Color.rgb(255,222,173);//Navajo White
		init_colors[1]= Color.rgb(0,0,255); //blue
		init_colors[2]= Color.rgb(0,255,0); //green
		init_colors[3]= Color.rgb(225,0,0); //red
		init_colors[4]= Color.rgb(0,0,0); //black
*/		
		//default color array
		
		bcolor[0]= Color.rgb(255,222,173);//Navajo White
		bcolor[1]= Color.rgb(0,0,255); //blue
		bcolor[2]= Color.rgb(0,255,0); //green
		bcolor[3]= Color.rgb(225,0,0); //red
		bcolor[4]= Color.rgb(255,255,0); //yellow
		bcolor[5]= Color.rgb(255,105,180); //hotpink
		bcolor[6]=Color.rgb(0,255,255); //cyan
		bcolor[7]=Color.rgb(210,105,30);//chocolate
		bcolor[8]=Color.rgb(160,32,240); //purple
		bcolor[9]=Color.rgb(255,255,255); //white
		bcolor[10]=Color.rgb(0,0,0); //black

		context = this;

		//Set Table Layout
		tl = new TableLayout(this);
		//tl.setStretchAllColumns(true);
		//tl.setShrinkAllColumns(true);
		
		//Set Table Layout
		t3 = new TableLayout(this);
		t3.setStretchAllColumns(true);
		t3.setShrinkAllColumns(true);
	
		//Set Linear Layout
		t2 = new LinearLayout(this);
		//min value
		mintext = new TextView(this);
		mintext.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9); 
		//mintext.setText("Min : "+String.valueOf(export1.min_value+"   "));
		mintext.setTypeface(null, Typeface.BOLD);
        //max value
		maxtext = new TextView(this);
		maxtext.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9); 
		//maxtext.setText("Max : "+String.valueOf(export1.max_value));
		maxtext.setTypeface(null, Typeface.BOLD);
		//add min value and max value 
		t2.addView(mintext);
		t2.addView(maxtext);
		
		//range for setting text for earray
		//range = export1.max_value -export1.min_value;
		
		//Set rows for t1
		rowtext = new TableRow(this);  
		rowtext1 = new TableRow(this);
		rowtext2 = new TableRow(this);
		//set rows for t3
		first = new TableRow(this); 
		
		//File button
		button4 = new TextView(this);
		button4.setTextColor(getResources().getColor(R.color.Black));
		button4.setText("File");
		button4.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button4.setGravity(Gravity.CENTER);  
		button4.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button4.setBackgroundResource(R.color.LightBlue);
	first.addView(button4);
		//OnClick listener for File button
		button4.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				loadFileList();
				showDialog(DIALOG_FILE_ENTRY1);
				//dec_value=0.1f;

			}
		});
		
		//reload button
		button1 = new TextView(this);
		button1.setTextColor(getResources().getColor(R.color.Black));
		button1.setText("Reload");	
		button1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button1.setGravity(Gravity.CENTER);  
		button1.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button1.setBackgroundResource(R.color.White);
		first.addView(button1);
		
		
		
		//Number select button
		button = new TextView(this);
		button.setTextColor(getResources().getColor(R.color.Black));
		button.setText("Lev");
		button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button.setGravity(Gravity.CENTER);  
		button.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button.setBackgroundResource(R.color.LightBlue);
		
		//Onclick Listener for Levels
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				showDialog(DIALOG_TEXT_ENTRY);
			}
		});
		first.addView(button);
		
		        
		//camera rotate button
		 
		tb = new TextView(this);
		tb.setTextColor(getResources().getColor(R.color.Black));
		tb.setText("RotM");
		tb.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		tb.setGravity(Gravity.CENTER);  
		tb.setTypeface(Typeface.SERIF, Typeface.BOLD);
		tb.setBackgroundResource(R.color.White);
        first.addView(tb);  
       // camera_flag
        tb.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
			if(camera_flag==0)
			{
				camera_flag=1;
				tb.setText("RotM");
				_VortexRenderer.setCameraflag(camera_flag);
				
			}
			else
			{
				camera_flag=0;
				tb.setText("RotA");
				_VortexRenderer.setCameraflag(camera_flag);
			} 

			}
		});

		
		//sur
		sur = new TextView(this);
		sur.setTextColor(getResources().getColor(R.color.Black));
		sur.setText("Sur");	
		sur.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		sur.setGravity(Gravity.CENTER);  
		sur.setTypeface(Typeface.SERIF, Typeface.BOLD);
		sur.setBackgroundResource(R.color.LightBlue);
		first.addView(sur);
		sur.setEnabled(false);
		
		//Onclick Listener for Levels
		sur.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				button1.setEnabled(false);
				button1.setBackgroundResource(R.color.Gray);
				button.setEnabled(false);
				button.setBackgroundResource(R.color.Gray);
				button2.setEnabled(false);
				button2.setBackgroundResource(R.color.Gray);
				Nocolor.setEnabled(false);
				Nocolor.setBackgroundResource(R.color.Gray);
				slice1.setEnabled(false);
				slice1.setBackgroundResource(R.color.Gray);
				slice2.setEnabled(false);
				slice2.setBackgroundResource(R.color.Gray);
				button4.setEnabled(false);
				button4.setBackgroundResource(R.color.Gray);
				/*button1.setVisibility(TextView.INVISIBLE);
				button.setVisibility(TextView.INVISIBLE);
				button2.setVisibility(TextView.INVISIBLE);
				Nocolor.setVisibility(TextView.INVISIBLE);
				slice1.setVisibility(TextView.INVISIBLE);
				slice2.setVisibility(TextView.INVISIBLE);
				button4.setVisibility(TextView.INVISIBLE);*/
				surf=1;
				//export1 = new export(getBaseContext(),mChosenFile);
				int p=0,q=0,r=0,rem=0,rem1=0;
				ArrayList<Float> pcolorsurf = new ArrayList<Float>();
				//color array
				for(int i=0;i<export1._intensity.length;i++)
				{
					rem=i%((export1.l2+1)*(export1.l3+1)); // remainder1
					p = (i-rem)/((export1.l2+1)*(export1.l3+1)); //quotient1
					rem = i%(export1.l3+1);    //remainder2
					q = (i-rem)/(export1.l3+1);  //quotient2
					r = i % (export1.l3+1);	
					//Log.v("p","p="+p+"q = "+q+"r= "+r);
					if((p%(export1.l1+1)<export1.l1) && (q%(export1.l2+1)<export1.l2)&&(r%(export1.l3+1)<export1.l3))
					{
						pcolorsurf.add(spcolor[(i*4)+0]);
						pcolorsurf.add(spcolor[(i*4)+1]);
						pcolorsurf.add(spcolor[(i*4)+2]);
						pcolorsurf.add(spcolor[(i*4)+3]);
					}
				}
				
				/*
				for(i=0;i<32;i++)
				{
					Log.v("pcolor","pcolorsurf="+pcolorsurf.get(i));
					Log.v("pcolor","pcolor="+export1._pcolors[i]);
					
				}
				*/
				
				//Log.v("intensity","intensity"+export1._intensity.length);
				//Log.v("l1","l1="+export1.l1+"l2="+export1.l2+"l3="+export1.l3);
				//Log.v("pcolor","pcolor"+export1._pcolors.length);
				//Log.v("colorarray","color array size"+pcolorsurf.size());
				float[] colorsurf = toPrimitiveArray(pcolorsurf);
				
				// Render the cube with surface option
				//cube1 = new cube1();
				
				//Renderer
				_VortexRenderer.setSurf_flag(surf);
				_VortexRenderer.setPcolor(colorsurf);
				
		      		
			}
		});
		
		
      //shade
		shade = new TextView(this);
		shade.setTextColor(getResources().getColor(R.color.Black));
		shade.setText("Shade");
		shade.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		shade.setGravity(Gravity.CENTER);  
		shade.setTypeface(Typeface.SERIF, Typeface.BOLD);
		shade.setBackgroundResource(R.color.White);
        first.addView(shade);  
       // camera_flag
        
        shade.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				surf=0;
				button4.setEnabled(true);
				button4.setBackgroundResource(R.color.LightBlue);
				button1.setEnabled(true);
				button1.setBackgroundResource(R.color.White);
				button.setEnabled(true);
				button.setBackgroundResource(R.color.LightBlue);
				button2.setEnabled(true);
				button2.setBackgroundResource(R.color.LightBlue);
				Nocolor.setEnabled(true);
				Nocolor.setBackgroundResource(R.color.White);
				slice1.setEnabled(true);
				slice1.setBackgroundResource(R.color.LightBlue);
				slice2.setEnabled(true);
				slice2.setBackgroundResource(R.color.White);
				
				/*button1.setVisibility(TextView.VISIBLE);
				button.setVisibility(TextView.VISIBLE);
				button2.setVisibility(TextView.VISIBLE);
				Nocolor.setVisibility(TextView.VISIBLE);
				slice1.setVisibility(TextView.VISIBLE);
				button4.setVisibility(TextView.VISIBLE);
				slice2.setVisibility(TextView.VISIBLE);*/
				
				if(shade_flag==0 && slice_flag!=0)
				//if(shade_flag==0)  // In point mode (display will be in points)
				{
					/*spcolor =null;
					spcolor = new float[_rc.length];*/
					//shade mode
					shade_flag=1;
					Log.v("log","shade=0 slice=1");
					for(i=3;i<_rc.length;i+=4)
					{
						if(_rc[i]==0.0f)
						{
							_rc[i]=0.0f;
						}
						else
						{	
							_rc[i]=0.2f;
						}
					}
						
						pointcube1.set_pcolors(_rc);
						shade.setText("Points");
						//spcolor = _rc;
						pointcube1.setSetvalue(shade_flag);
				}
				else if (shade_flag==1 && slice_flag!=0)
				{
					
					//spcolor =null;
					//spcolor = new float[_rc.length];
					//point mode
					shade_flag=0;
					Log.v("log","shade=1 slice=0");
					for(i=3;i<_rc.length;i+=4)
					{
						
						if(_rc[i]==0.0f)
						{
							_rc[i]=0.0f;
						}
						else
						{
							_rc[i]=1.0f;
						}
					}
					pointcube1.set_pcolors(_rc);
					shade.setText("Shade");
					//spcolor = _rc;
					pointcube1.setSetvalue(shade_flag);
				}
				else if (shade_flag==0 && slice_flag==0)
				{
					//spcolor =null;
					//spcolor = new float[_pcolors.length];
					//shade mode
					shade_flag=1; 
					for(i=3;i<export1._pcolors.length;i+=4)
					{
						if(export1._pcolors[i]==0.0f)
						{
							export1._pcolors[i]=0.0f;
						}
						else
						{	
							export1._pcolors[i]=0.2f;
						}
					}
					pointcube1.set_pcolors(export1._pcolors);
					shade.setText("Points");
					//spcolor = export1._pcolors;
					pointcube1.setSetvalue(shade_flag);
					
				}
				else if (shade_flag==1 && slice_flag==0)
				{
					//spcolor =null;
					//spcolor = new float[_pcolors.length];
					//point point
					shade_flag=0;
					for(i=3;i<export1._pcolors.length;i+=4)
					{
						if(export1._pcolors[i]==0.0f)
						{
							export1._pcolors[i]=0.0f;
						}
						else
						{	
							export1._pcolors[i]=1.0f;
						}
					}
					pointcube1.set_pcolors(export1._pcolors);
					shade.setText("Shade");
					//spcolor = export1._pcolors;
					pointcube1.setSetvalue(shade_flag);
					
				}
				
				_VortexRenderer.setSurf_flag(surf);
				_VortexRenderer.setPointcube(pointcube1);
		}});
        
    
		
		  
		
		
		//reset button
		button2 = new TextView(this);
		button2.setTextColor(getResources().getColor(R.color.Black));
		button2.setText("Reset");
		button2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button2.setGravity(Gravity.CENTER);  
		button2.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button2.setBackgroundResource(R.color.LightBlue);
		first.addView(button2);

		//On click listener for reset
		button2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				rowtext.removeAllViews();
				rowtext1.removeAllViews();
				rowtext2.removeAllViews();
				colorval=0;
				colorval1=0;
				_levels=4;	
				export1._pcolors = (float[])copy_c.clone();
				if(shade_flag==1) //if shade mode is on
				{
					
					for(i=0;i<(export1._pcolors.length/4);i++)
					{
						export1._pcolors[(4*i)+3]=0.2f;
					}
				}
				else if(shade_flag==0)//point mode is on
				{
					for(i=0;i<(export1._pcolors.length/4);i++)
					{
						export1._pcolors[(4*i)+3]=1.0f;
					}
				}
				export1._pvertices = (float[])copy_v.clone();
				export1._pindicesnew = copy_i.clone();
				export1._noVertices =copy_noVertices; 
				pointcube1.set_pcolors(export1._pcolors);
				pointcube1.set_pvertices(export1._pvertices);
				pointcube1.set_noVertices(export1._noVertices);
				pointcube1.set_pindices(export1._pindicesnew);
				//reset surface settings
				_VortexRenderer.setL1(export1.l1);
				_VortexRenderer.setL1(export1.l2);
				_VortexRenderer.setL1(export1.l3);
				_VortexRenderer.setIncx(export1.incx);
				_VortexRenderer.setIncy(export1.incy);
				_VortexRenderer.setIncz(export1.incz);
				
				_VortexRenderer.setPointcube(pointcube1);
				createB(_levels);
				copy_xmax = export1.getMaxXValue(copy_v);
				copy_ymax = export1.getMaxYValue(copy_v);
				copy_zmax = export1.getMaxZValue(copy_v);
				copy_xmin = export1.getMinXValue(copy_v);
		        copy_ymin = export1.getMinYValue(copy_v);
		        copy_zmin = export1.getMinZValue(copy_v);
				//dec_value=0.1f;
				slice_flag=0;
				spcolor = export1._pcolors;  //for surface mode
				for(i=0;i<(spcolor.length/4);i++)
				{
					spcolor[(4*i)+3]=0.2f;
				}
			}
		});
		
		 //Nocolor button
		Nocolor = new TextView(this);
		Nocolor.setText("Hold");
		Nocolor.setTextColor(getResources().getColor(R.color.Black));
		Nocolor.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		Nocolor.setGravity(Gravity.CENTER);  
		Nocolor.setTypeface(Typeface.SERIF, Typeface.BOLD);
		Nocolor.setBackgroundResource(R.color.White);
		first.addView(Nocolor);
		
		
		//slice settings button
		slice1 = new TextView(this);
		slice1.setTextColor(getResources().getColor(R.color.Black));
		slice1.setText("Set Slice");
		slice1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		slice1.setGravity(Gravity.CENTER);  
		slice1.setTypeface(Typeface.SERIF, Typeface.BOLD);
		slice1.setBackgroundResource(R.color.LightBlue);
		first.addView(slice1);
		
		//Onclick listener for slice settings
		slice1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				showDialog(DIALOG_SLICE_ENTRY);	
			}
		});
		
		
		//slicex or slicey or slicez button
		slice2 = new TextView(this);
		slice2.setEnabled(false);
		slice2.setClickable(false);
		slice2.setTextColor(getResources().getColor(R.color.Black));
		slice2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		slice2.setGravity(Gravity.CENTER);  
		slice2.setTypeface(Typeface.SERIF, Typeface.BOLD);
		slice2.setBackgroundResource(R.color.White);
		first.addView(slice2);
		
		//Onclick listener for slice
		slice2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				// intensity checkbox checked
				if(ck4.isChecked())
				{
					x = ed1.getText().toString();
					if(x.trim().equalsIgnoreCase("") || x.trim().length()==0)
					{
						Toast.makeText(Vortex.this,
								"Enter an Intensity Value",
								Toast.LENGTH_LONG).show();
					}
					else
					{
						String slicetext = slice2.getText().toString();
						slicex(slicetext);
					}
				}
				else
				{
					String slicetext = slice2.getText().toString();
					slicex(slicetext);

				}
			}
		});
		
		
		//Onclick listener for No color
		Nocolor.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				
				/*int vl1 = 0, vl2 = 0, vl3=0;
				float vl11=0.0f, vl22=0.0f, vl33=0.0f;

				vl11=(Math.round((export1.getMaxXValue(_pvertices)-export1.getMinXValue(_pvertices))/export1.incx)*10000.0f)/10000.0f;
				vl22=(Math.round((export1.getMaxYValue(_pvertices)-export1.getMinYValue(_pvertices))/export1.incx)*10000.0f)/10000.0f;
				vl33=(Math.round((export1.getMaxZValue(_pvertices)-export1.getMinZValue(_pvertices))/export1.incx)*10000.0f)/10000.0f;
				vl1 = (int)vl11;
				vl2 = (int)vl22;
				vl3 = (int)vl33;*/

				
				//Log.v("reload", "reload");
				int f_reload =0, f_reload1=0;
				for(int i=0;i<_levels+1;i++)
				{
					if( earray[i].getText().toString().length() == 0 || earray[i].getText().toString() == "" || (earray[i].getText().toString()).trim().equalsIgnoreCase(""))   
					{  
						f_reload=f_reload+1;

					}
					else if(Float.parseFloat(earray[i].getText().toString()) < 0  || Float.parseFloat(earray[i].getText().toString()) > export1.max_value )

					{ 
						f_reload=f_reload+1;
					}
				}

				if(f_reload >0)
				{
					Toast.makeText(Vortex.this,
							"Enter a value between "+export1.min_value+ " and "+ export1.max_value,
							Toast.LENGTH_LONG).show();
				}
				else if(f_reload >0)
				{
					Toast.makeText(Vortex.this,
							"The values are not in order.",
							Toast.LENGTH_LONG).show();
				}
				else
				{
					int hcount =0;
					float a1=0.0f;
					spcolor=null;
					spcolor=new float[export1._pcolors.length];
					for(int k=0;k<export1._noVertices;k++)
					{
						for(int m=0;m<_levels;m++)
						{
							float r1=0.0f,g1=0.0f,b1=0.0f;
							if(shade_flag==0)
			                 {
			                	 aa=1.0f;
			                 }
			                 else
			                 {
			                	aa=0.2f; 
			                 }
							if(Integer.parseInt((barray[m].getTag().toString()))==0)
							{
							    r1 = Color.red(bcolor[m])/255f;
								g1 = Color.green(bcolor[m])/255f;
								b1 = Color.blue(bcolor[m])/255f;
								//a1 = Color.alpha(bcolor[m])/255f;
								a1=aa;
								   
							}
							else
							{	
					
							    r1 = Color.red(Integer.parseInt((barray[m].getTag().toString())))/255f;
								g1 = Color.green(Integer.parseInt((barray[m].getTag().toString())))/255f;
								b1 = Color.blue(Integer.parseInt((barray[m].getTag().toString())))/255f;
								//a1 = Color.alpha(Integer.parseInt((barray[m].getTag().toString())))/255f;
								a1=aa;
								
							}
							float hol1=0.0f;float hol2=0.0f;
							hol1=Float.parseFloat(earray[m].getText().toString());
							hol2=Float.parseFloat(earray[m+1].getText().toString());
						    //checking bounds for min to include minimum value too...
							if(m==0) hol1=hol1-(hol1/2);
							hol1=(Math.round(hol1*10000.0f))/10000.0f;
							hol2=(Math.round(hol2*10000.0f))/10000.0f;
							if(export1._intensity[k]>hol1 &&  export1._intensity[k]<= hol2)
							{	
								
								

								if(ckarray[m].isChecked())
								{
									export1._pcolors[hcount] = 0.0f;
									export1._pcolors[hcount+1]=0.0f;
									export1._pcolors[hcount+2]=0.0f;
									export1._pcolors[hcount+3] =0.0f;
									
									spcolor[hcount] = 0.0f;
									spcolor[hcount+1]=0.0f;
									spcolor[hcount+2]=0.0f;
									spcolor[hcount+3] =0.0f;
									
									
								}
								else
								{
									export1._pcolors[hcount] = r1;
									export1._pcolors[hcount+1]= g1;
									export1._pcolors[hcount+2]= b1;
									export1._pcolors[hcount+3] = a1;
									
									spcolor[hcount] = r1;
									spcolor[hcount+1]=g1;
									spcolor[hcount+2]=b1;
									spcolor[hcount+3] =0.2f;
									
								}
							}
						}
						hcount=hcount+4;                  
					}
					
					pointcube1.set_pcolors(export1._pcolors);
					pointcube1.set_pvertices(export1._pvertices);
					pointcube1.set_noVertices(export1._noVertices);
					pointcube1.set_pindices(export1._pindicesnew);
					//_VortexRenderer.setPcolor(export1._pcolors);
					//surface settings
					_VortexRenderer.setL1(export1.l1);
					_VortexRenderer.setL1(export1.l2);
					_VortexRenderer.setL1(export1.l3);
					_VortexRenderer.setIncx(export1.incx);
					_VortexRenderer.setIncy(export1.incy);
					_VortexRenderer.setIncz(export1.incz);
					_VortexRenderer.setPointcube(pointcube1);
					//settings for slice
					copy_xmax = export1.getMaxXValue(copy_v);
					copy_ymax = export1.getMaxYValue(copy_v);
					copy_zmax = export1.getMaxZValue(copy_v);
					//dec_value=0.1f;
					slice_flag=0;
					
				}
			}
			
		});
		
		//Table Layout params
		params1 = new TableRow.LayoutParams(); 
		params1.width=0;
		params = new TableRow.LayoutParams();	
		rowInTableLp = new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		rowInTableLp.weight =0.3f;
		//Create the Initial Color Table Display
		//createB(_levels);

		//add to layout 
		t3.addView(first);
		tl.addView(rowtext);
		tl.addView(rowtext1);
		tl.addView(rowtext2);
		
		t2.setGravity(Gravity.TOP | Gravity.LEFT );
		t2.setVisibility(LinearLayout.GONE);
		t3.setGravity(Gravity.CENTER);
		tl.setGravity(Gravity.CENTER);
		tl.setVisibility(TableLayout.GONE);
		//add t1,t2,t3 to linear layout
		layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setGravity(Gravity.BOTTOM | Gravity.CENTER);
		layout.addView(t2);
		layout.addView(t3);
		layout.addView(tl);
		
       //add linearlayout 'layout' to contentview
		this.addContentView(layout,
				new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));



		// Onclick Listener for Reload
		button1.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View view) 
			{
				int f_reload =0, f_reload1=0;
				for(int i=0;i<_levels+1;i++)
				{
					if( earray[i].getText().toString().length() == 0 || earray[i].getText().toString() == "" || (earray[i].getText().toString()).trim().equalsIgnoreCase(""))   
					{  
						f_reload=f_reload+1;

					}
					else if(Float.parseFloat(earray[i].getText().toString()) < 0  || Float.parseFloat(earray[i].getText().toString()) > export1.max_value )
					{ 

						f_reload=f_reload+1;
					}
					
				}

				if(f_reload >0)
				{
					Toast.makeText(Vortex.this,
							"enter a value between "+export1.min_value+ "and"+ export1.max_value,
							Toast.LENGTH_LONG).show();
				}
				else if(f_reload1 >0)
				{
					Toast.makeText(Vortex.this,
							"Please check if values are in order",
							Toast.LENGTH_LONG).show();
				}
				else
				{
					int rcount =0;
					spcolor=null;
					spcolor=new float[export1._pcolors.length];
					for(int k=0;k<export1._noVertices;k++)
					{
						for(int m=0;m<_levels;m++)
						{
							float r1=0.0f,g1=0.0f,b1=0.0f,a1=0.0f;
			                 if(shade_flag==0)
			                 {
			                	 aa=1.0f;
			                 }
			                 else
			                 {
			                	aa=0.2f; 
			                 }
							//if default color is choosen
							if(Integer.parseInt((barray[m].getTag().toString()))==0)
							{
					    
							    r1 = Color.red(bcolor[m])/255f;
								g1 = Color.green(bcolor[m])/255f;
								b1 = Color.blue(bcolor[m])/255f;
								//a1 = Color.alpha(bcolor[m])/255f;
								a1 = aa;
							    
							}
							//if a color is selected
							else
							{	
								r1 = Color.red(Integer.parseInt((barray[m].getTag().toString())))/255f;
								g1 = Color.green(Integer.parseInt((barray[m].getTag().toString())))/255f;
								b1 = Color.blue(Integer.parseInt((barray[m].getTag().toString())))/255f;
								//a1 = Color.alpha(Integer.parseInt((barray[m].getTag().toString())))/255f;
								//a1=0.2f;
								a1=aa;
							}

							float rel1=0.0f;float rel2=0.0f;
							rel1=Float.parseFloat(earray[m].getText().toString());
							rel2=Float.parseFloat(earray[m+1].getText().toString());
						    //checking bounds for min to include minimum value too...
							if(m==0) rel1=rel1-(rel1/2);
							rel1=(Math.round(rel1*10000.0f))/10000.0f;
							rel2=(Math.round(rel2*10000.0f))/10000.0f;
							if(export1._intensity[k]>rel1 &&  export1._intensity[k]<= rel2)
							{								
								export1._pcolors[rcount] = r1;
								export1._pcolors[rcount+1]= g1;
								export1._pcolors[rcount+2]= b1;
								export1._pcolors[rcount+3] = a1;
								spcolor[rcount] = r1;
								spcolor[rcount+1]=g1;
								spcolor[rcount+2]=b1;
								spcolor[rcount+3] =0.2f;
							}
						}
						rcount=rcount+4;                  
					}
					pointcube1.set_pcolors(export1._pcolors);
					pointcube1.set_pvertices(export1._pvertices);
					pointcube1.set_noVertices(export1._noVertices);
					pointcube1.set_pindices(export1._pindicesnew);
					//surface settings
					_VortexRenderer.setL1(export1.l1);
					_VortexRenderer.setL1(export1.l2);
					_VortexRenderer.setL1(export1.l3);
					_VortexRenderer.setIncx(export1.incx);
					_VortexRenderer.setIncy(export1.incy);
					_VortexRenderer.setIncz(export1.incz);
					
					_VortexRenderer.setPointcube(pointcube1);
					
					//settings for slice
					copy_xmax = export1.getMaxXValue(copy_v);
					copy_ymax = export1.getMaxYValue(copy_v);
					copy_zmax = export1.getMaxZValue(copy_v);
					//dec_value=0.1f;
					slice_flag=0;
					colorval=0;
					
					//spcolor = export1._pcolors;
				}
			}
		});
	}

	

	
	
	
	//Called to RESET renderer

	//Called to CUSTOMIZE COLOR
	int i;
	public void createB(int no)
	{
		
		int size = 8; //text size
		rowtext.removeAllViews();
		rowtext1.removeAllViews();
		rowtext2.removeAllViews();
		barray = new TextView[no+1];
		earray = new EditText[no+1];
		ckarray = new CheckBox[no+1]; 
		
	    float range1 = range/no;
	    float erange=0.0f;
		int noo = no+1;	
		for(i=0;i<noo;i++)
		{  
			
			erange = export1.min_value + (i*range1);
			//textviews in row2
			barray[i] = new TextView(this);
			//barray[i].setWidth(rowtext1.getWidth()/(no+1));
			barray[i].setWidth(0);
			barray[i].setHeight(5);
			//barray.[i].setWidth(30);
			//barray[i].setBackgroundResource(R.color.Blue);
			//barray[i].setBackgroundResource(bcolor[i]);
			
			barray[i].setBackgroundColor(bcolor[i]);
			barray[i].setId(i);
			barray[i].setTag(colorval);
			//edittexts in row3
			earray[i] = new EditText(this);
			earray[i].setPadding(0, 0, 0, 0);
			
			//earray[i].setId(i+10);
			earray[i].setTextColor(getResources().getColor(R.color.White));
			earray[i].setBackgroundResource(R.color.Black);
			earray[i].setTextSize(TypedValue.COMPLEX_UNIT_DIP, size); 
			earray[i].setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
			//InputFilter FilterArray = new InputFilter[1];
			//FilterArray[0] = new InputFilter.LengthFilter(3);
			//earray[i].setFilters(FilterArray);
			earray[i].setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
			earray[i].setGravity(Gravity.LEFT |Gravity.TOP);
			earray[i].setText(String.valueOf(erange));
			earray[i].setMaxLines(1);
			/*//do not allow soft-keyboard to show
			earray[i].setOnTouchListener(new View.OnTouchListener()
			{

				@Override
				public boolean onTouch(View arg0, MotionEvent arg1) {
					// TODO Auto-generated method stub
					return true;
				} 
			
			});*/
			
			//checkbox
			ckarray[i] = new CheckBox(this);  
			ckarray[i].setButtonDrawable(R.drawable.checkbox_selector); 
			ckarray[i].setPadding(0, 0, 0, 0);
			//add to row
			rowtext.addView(barray[i],rowInTableLp);
			Log.v("barray width","barrray width"+barray[i].getWidth());
			rowtext1.addView(earray[i],rowInTableLp);
			rowtext2.addView(ckarray[i],rowInTableLp);
			if(i==(noo-1))
			{
				barray[i].setBackgroundResource(R.color.Black);
				rowtext2.removeViewInLayout(ckarray[i]);
			}
             //on click listener for color array
			barray[i].setOnClickListener(new View.OnClickListener() 
			{
				public void onClick(View cbutton) 
				{
					Log.v("clicked","clicked");
					int color = PreferenceManager.getDefaultSharedPreferences
					(Vortex.this).getInt(COLOR_PREFERENCE_KEY,Color.WHITE);
					new ColorPickerDialog(Vortex.this, Vortex.this,color).show();
					//barray[cbutton.getId()].setBackgroundColor(colorval);			
					colorval1=cbutton.getId();
				}
			});
		}


	}
	
	
   float[] _grouparray;
	
	
	
	
	//Called when color changes
	@Override
	public void colorChanged(int color ) 
	{
		PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(COLOR_PREFERENCE_KEY, color).commit();

		colorval = color;
		barray[colorval1].setBackgroundColor(colorval);
		barray[colorval1].setTag(colorval);
	}
	
	ArrayList<Float> pcolorsList = new ArrayList<Float>();
	ArrayList <Float> pverticesList = new ArrayList <Float>();
	
	float[] newvertices,newpcolors;
	//Called to Slice Surface
	
	String conv_v[] , conv_c[];
	float _rv[] , _rc[],_ri[];
	
	
	
	//slicex ************************************************************************
	int length;
	float varray[];
	float carray[];
	float iarray[];
	boolean no_slice_flag=true;
	public void slicex(String slicetext)
	{
		Log.v("slicetext","slicetext="+slicetext); 
		int j=0;
		if(slice_flag>0)
		{
			varray = new float[_rv.length];
			carray = new float[_rc.length];
			iarray = new float[_ri.length];
			varray = (float[])_rv.clone();
			carray = (float[])_rc.clone();
			iarray =(float[])_ri.clone();
		}
		else
		{
			varray = new float[export1._pvertices.length];
			carray = new float[export1._pcolors.length];
			iarray = new float[export1._intensity.length];
			varray = (float[])export1._pvertices.clone();
			carray = (float[])export1._pcolors.clone();
			iarray = (float[])export1._intensity.clone();
		}
		
		if(slicetext == "CutX")
		{
			if(copy_xmax<=copy_xmin)
			{
				no_slice_flag=false;
				Toast.makeText(this, "Choose alternate slice direction or reset", Toast.LENGTH_SHORT).show();		
			}
			else
			{	
				no_slice_flag=true;
				j=0;
				copy_xmax=(Math.round(copy_xmax*10000.0f))/10000.0f;
				copy_max = copy_xmax;
				//dec_value = export1.incx;
				copy_max=(Math.round(copy_max*10000.0f))/10000.0f;
				export1.incx=(Math.round(export1.incx*10000.0f))/10000.0f;
				copy_xmax=copy_xmax-export1.incx;
				//copy_xmax = Math.round(copy_xmax);
				//Log.v("incx","incx="+export1.incx);;
				//Log.v("copymax after decrementing","copymax="+copy_max);
				//Log.v("copy ","copxmax="+copy_xmax + " copy_ymax="+copy_ymax+ " copy_zmax"+copy_zmax);
			}
		}
		else if(slicetext == "CutY")
		{
			if(copy_ymax<=copy_ymin)
			{
				no_slice_flag=false;
				Toast.makeText(this, "Choose alternate slice direction or reset", Toast.LENGTH_SHORT).show();
			}
			else
			{
				no_slice_flag=true;
				j=1;
				copy_ymax=(Math.round(copy_ymax*10000.0f))/10000.0f;
				copy_max = copy_ymax;
				//dec_value = export1.incy;
				copy_max=(Math.round(copy_max*10000.0f))/10000.0f;
				export1.incy=(Math.round(export1.incy*10000.0f))/10000.0f;
				copy_ymax=copy_ymax-export1.incy;
				//Log.v("copy ","copxmax="+copy_xmax + " copy_ymax="+copy_ymax+ " copy_zmax"+copy_zmax);
				
			}
		}
		else if(slicetext == "CutZ")
		{
			if(copy_zmax <= copy_zmin)
			{
				no_slice_flag=false;
				Toast.makeText(this, "Choose alternate slice direction or reset", Toast.LENGTH_SHORT).show();
			}
			else
			{	
				no_slice_flag=true;
				j=2;
				copy_zmax=(Math.round(copy_zmax*10000.0f))/10000.0f;
				copy_max = copy_zmax;
				//dec_value = export1.incz;
				copy_max=(Math.round(copy_max*10000.0f))/10000.0f;
				export1.incz=(Math.round(export1.incz*10000.0f))/10000.0f;
				copy_zmax=copy_zmax-export1.incz;
				//Log.v("copy ","copxmax="+copy_xmax + " copy_ymax="+copy_ymax+ " copy_zmax"+copy_zmax);
			}
		}
		
		
		
	
		if(no_slice_flag==true)
		{	
			ArrayList<Float> colors = new ArrayList<Float>();
			ArrayList <Float> vertices = new ArrayList <Float>();
			ArrayList <Float> ivalue = new ArrayList <Float>();
			ArrayList <Integer> t_intensity = new ArrayList <Integer>();
			ArrayList <Integer> qarray = new ArrayList <Integer>();
			
			spcolor = null;
			spcolor = new float[export1._pcolors.length];
			
			Log.v("copy ","copxmax="+copy_xmax + " copy_ymax="+copy_ymax+ " copy_zmax"+copy_zmax);
			Log.v("varray ","varray="+varray.length);

			int q=0,q1=0;  
			for(int i =j;i<varray.length;i+=3)
			{
				q =((i-j)/3+1);
				//qarray.add(q);

				if(ck4.isChecked()&& shade_flag==0)	//Intensity hold option, Points mode
				{
					if(varray[i]<copy_max)	//Pass values with increment condition
					{
						
						q1=(q-1)*4;
						
						vertices.add(varray[3*(q-1)+0]);
						vertices.add(varray[3*(q-1)+1]);
						vertices.add(varray[3*(q-1)+2]);
	
						colors.add(carray[q1]);
						colors.add(carray[q1+1]);
						colors.add(carray[q1+2]);
						//colors.add(carray[q1+3]);
						colors.add(1.0f);
			            ivalue.add(iarray[q-1]);
			            
						//spcolor updation for Surface view
			            int q2=(q-1)*3;
						int q2x=(int) ((varray[q2+0]+0.5f)/export1.incx);
						int q2y=(int) ((varray[q2+1]+0.5f)/export1.incy);
						int q2z=(int) ((varray[q2+2]+0.5f)/export1.incz);
						
						int qq1=(q2x*(export1.l2+1)*(export1.l3+1)+(q2y*(export1.l3+1))+q2z);
						//Log.v("qq1","qq1= "+qq1);
						
						spcolor[(4* qq1+0)]=carray[q1];
			            spcolor[(4* qq1+1)]=carray[q1+1];
			            spcolor[(4* qq1+2)]=carray[q1+2];
			            spcolor[(4* qq1+3)]=0.2f;
						
			            /*
			            spcolor[(q1+0)]=carray[q1];
			            spcolor[(q1+1)]=carray[q1+1];
			            spcolor[(q1+2)]=carray[q1+2];
			            spcolor[(q1+3)]=0.2f;
			            */
			            
					}
					else if(iarray[q-1]>  Float.parseFloat(x))	//Pass values with intensity hold condition
					{
						q1=(q-1)*4;
						vertices.add(varray[3*(q-1)+0]);
						vertices.add(varray[3*(q-1)+1]);
						vertices.add(varray[3*(q-1)+2]);
	
						colors.add(carray[q1]);
						colors.add(carray[q1+1]);
						colors.add(carray[q1+2]);
						//colors.add(carray[q1+3]);
						colors.add(1.0f);
						ivalue.add(iarray[q-1]);
						t_intensity.add(q-1);
						//t_intensity.add(iarray.length-1);
						
						//spcolor updation for Surface view
						int q2=(q-1)*3;
						int q2x=(int) ((varray[q2+0]+0.5f)/export1.incx);
						int q2y=(int) ((varray[q2+1]+0.5f)/export1.incy);
						int q2z=(int) ((varray[q2+2]+0.5f)/export1.incz);
						
						int qq1=(q2x*(export1.l2+1)*(export1.l3+1)+(q2y*(export1.l3+1))+q2z);
						spcolor[(4* qq1+0)]=carray[q1];
			            spcolor[(4* qq1+1)]=carray[q1+1];
			            spcolor[(4* qq1+2)]=carray[q1+2];
			            spcolor[(4* qq1+3)]=0.2f;
						
			            /*
			            spcolor[(q1+0)]=carray[q1];
			            spcolor[(q1+1)]=carray[q1+1];
			            spcolor[(q1+2)]=carray[q1+2];
			            spcolor[(q1+3)]=0.2f;
			            */

					}
				}
				else if(ck4.isChecked()&& shade_flag==1)	//Intensity hold option
				{
					if(varray[i]<copy_max)	//Pass values with increment condition
					{
						q1=(q-1)*4;
						
						vertices.add(varray[3*(q-1)+0]);
						vertices.add(varray[3*(q-1)+1]);
						vertices.add(varray[3*(q-1)+2]);
	
						colors.add(carray[q1]);
						colors.add(carray[q1+1]);
						colors.add(carray[q1+2]);
						//colors.add(carray[q1+3]);
						colors.add(0.2f);
			            ivalue.add(iarray[q-1]);
			            
			            
						//spcolor updation for Surface view
						int q2=(q-1)*3;
						int q2x=(int) ((varray[q2+0]+0.5f)/export1.incx);
						int q2y=(int) ((varray[q2+1]+0.5f)/export1.incy);
						int q2z=(int) ((varray[q2+2]+0.5f)/export1.incz);
						
						int qq1=(q2x*(export1.l2+1)*(export1.l3+1)+(q2y*(export1.l3+1))+q2z);
						//Log.v("qq1","qq1= "+qq1);
						spcolor[(4* qq1+0)]=carray[q1];
			            spcolor[(4* qq1+1)]=carray[q1+1];
			            spcolor[(4* qq1+2)]=carray[q1+2];
			            spcolor[(4* qq1+3)]=0.2f;
			            

			            /*
			            spcolor[(q1+0)]=carray[q1];
			            spcolor[(q1+1)]=carray[q1+1];
			            spcolor[(q1+2)]=carray[q1+2];
			            spcolor[(q1+3)]=0.2f;
			            */
			            

					}
					else if(iarray[q-1]>  Float.parseFloat(x))	//Pass values with intensity hold condition
					{
						q1=(q-1)*4;
						vertices.add(varray[3*(q-1)+0]);
						vertices.add(varray[3*(q-1)+1]);
						vertices.add(varray[3*(q-1)+2]);
	
						colors.add(carray[q1]);
						colors.add(carray[q1+1]);
						colors.add(carray[q1+2]);
						//colors.add(carray[q1+3]);
						colors.add(0.2f);
						ivalue.add(iarray[q-1]);
						t_intensity.add(q-1);
						//t_intensity.add(iarray.length-1);
						
						//spcolor updation for Surface view
						int q2=(q-1)*3;
						int q2x=(int) ((varray[q2+0]+0.5f)/export1.incx);
						int q2y=(int) ((varray[q2+1]+0.5f)/export1.incy);
						int q2z=(int) ((varray[q2+2]+0.5f)/export1.incz);
						
						int qq1=(q2x*(export1.l2+1)*(export1.l3+1)+(q2y*(export1.l3+1))+q2z);
						spcolor[(4* qq1+0)]=carray[q1];
			            spcolor[(4* qq1+1)]=carray[q1+1];
			            spcolor[(4* qq1+2)]=carray[q1+2];
			            spcolor[(4* qq1+3)]=0.2f;
	
						/*
						spcolor[(q1+0)]=carray[q1];
			            spcolor[(q1+1)]=carray[q1+1];
			            spcolor[(q1+2)]=carray[q1+2];
			            spcolor[(q1+3)]=0.2f;
			            */
			            
	            
					}

					
				}
				else	//Intensity unhold
				{
					if(varray[i]<copy_max && shade_flag==0)
					{
						q1=(q-1)*4;
						
						vertices.add(varray[3*(q-1)+0]);
						vertices.add(varray[3*(q-1)+1]);
						vertices.add(varray[3*(q-1)+2]);
	
						colors.add(carray[q1]);
						colors.add(carray[q1+1]);
						colors.add(carray[q1+2]);
						//colors.add(carray[q1+3]);
						colors.add(1.0f);
			            ivalue.add(iarray[q-1]);
			            
						//spcolor updation for Surface view
						int q2=(q-1)*3;
						int q2x=(int) ((varray[q2+0]+0.5f)/export1.incx);
						int q2y=(int) ((varray[q2+1]+0.5f)/export1.incy);
						int q2z=(int) ((varray[q2+2]+0.5f)/export1.incz);
						
						int qq1=(q2x*(export1.l2+1)*(export1.l3+1)+(q2y*(export1.l3+1))+q2z);
						//Log.v("qq1","qq1= "+qq1);
						
						spcolor[(4* qq1+0)]=carray[q1];
			            spcolor[(4* qq1+1)]=carray[q1+1];
			            spcolor[(4* qq1+2)]=carray[q1+2];
			            spcolor[(4* qq1+3)]=0.2f;
			            
	
			            /*
			            spcolor[(q1+0)]=carray[q1];
			            spcolor[(q1+1)]=carray[q1+1];
			            spcolor[(q1+2)]=carray[q1+2];
			            spcolor[(q1+3)]=0.2f;
			            */
			            

					}
					else if(varray[i]<copy_max && shade_flag==1)
					{
						q1=(q-1)*4;
						
						vertices.add(varray[3*(q-1)+0]);
						vertices.add(varray[3*(q-1)+1]);
						vertices.add(varray[3*(q-1)+2]);
	
						colors.add(carray[q1]);
						colors.add(carray[q1+1]);
						colors.add(carray[q1+2]);
						//colors.add(carray[q1+3]);
						colors.add(0.2f);
			            ivalue.add(iarray[q-1]);
			            
			            

						//spcolor updation for Surface view
						int q2=(q-1)*3;
						int q2x=(int) ((varray[q2+0]+0.5f)/export1.incx);
						int q2y=(int) ((varray[q2+1]+0.5f)/export1.incy);
						int q2z=(int) ((varray[q2+2]+0.5f)/export1.incz);
						
						int qq1=(q2x*(export1.l2+1)*(export1.l3+1)+(q2y*(export1.l3+1))+q2z);
						Log.v("qq1","qq1= "+qq1);
						spcolor[(4* qq1+0)]=carray[q1];
			            spcolor[(4* qq1+1)]=carray[q1+1];
			            spcolor[(4* qq1+2)]=carray[q1+2];
			            spcolor[(4* qq1+3)]=0.2f;
			            

			            /*
			            spcolor[(q1+0)]=carray[q1];
			            spcolor[(q1+1)]=carray[q1+1];
			            spcolor[(q1+2)]=carray[q1+2];
			            spcolor[(q1+3)]=0.2f;
			            */
			            

					}
				}
			}
		
		_rv = toPrimitiveArray(vertices);
		_rc = toPrimitiveArray(colors);
		_ri =toPrimitiveArray(ivalue);
		

		
		float vxmax ,vymax ,vzmax ,vxmin,vymin,vzmin=0.0f;
		vxmax = export1.getMaxXValue(_rv);
		vymax = export1.getMaxYValue(_rv);
		vzmax = export1.getMaxZValue(_rv);
		vxmin = export1.getMinXValue(_rv);
		vymin = export1.getMinYValue(_rv);
		vzmin = export1.getMinZValue(_rv);
/*		Log.v("_rv","_rv"+_rv.length);
		Log.v("_ri","ri"+_ri.length);
		Log.v("values"," vxmax="+vxmax+" vymax="+vymax+" vzmax="+vzmax+" vxmin="+vxmin+" vymin="+vymin+" vzmin="+vzmin);
*/		
		//indices 
		int vl1 = 0 , vl2 = 0 ,vl3=0;
		float vl11=0.0f, vl22=0.0f, vl33=0.0f;

		vl11=((vxmax-vxmin)/export1.incx);
		vl22=((vymax-vymin)/export1.incx);
		vl33=((vzmax-vzmin)/export1.incx);
		vl11=(Math.round(vl11*10000.0f))/10000.0f;
		vl22=(Math.round(vl22*10000.0f))/10000.0f;
		vl33=(Math.round(vl33*10000.0f))/10000.0f;
		//Log.v("vl values"," vl11="+vl11+" vl22="+vl22+" vl33="+vl33);
		vl1 = (int)vl11;
		vl2 = (int)vl22;
		vl3 = (int)vl33;
		
		_VortexRenderer.setL1(vl1);
		_VortexRenderer.setL1(vl2);
		_VortexRenderer.setL1(vl3);
		/*
		int scount=0;
		spcolor =null;
		spcolor =new float[export1._pcolors.length];
		for(float sx=0;sx<=vl1;sx+=1)
		{
			sx=(Math.round(sx*10000.0f))/10000.0f; 
			for(float sy=0;sy<=vl2;sy+=1)
			{
				sy=(Math.round(sy*10000.0f))/10000.0f; 
				for(float sz=0;sz<=vl3;sz+=1)
				{
					sz=(Math.round(sz*10000.0f))/10000.0f;
					spcolor[scount+0] = _rc[scount+0];
					spcolor[scount+1] = _rc[scount+1];
					spcolor[scount+2] = _rc[scount+2];
					spcolor[scount+3] = _rc[scount+3];
					scount =scount+4;
				}
			}
		}
		/*
		 Log.v("spcolor count"," spcolor count"+  spcolor.length);
		Log.v("_ri"," _ri="+  _ri.length);
		Log.v("_rv"," _rv="+  _rv.length);
		
		/*ArrayList<Float> _rip = new ArrayList<Float>();
		for(i=0;i<_ri.length;i++)
		{
			if(_rv[i]<copy_xmax && _rv[i+1]<copy_ymax && _rv[i+2]<copy_zmax)
			{
				_rip.add(_rv[i]);
			}
		}*/
		//Log.v("vl values"," vl1="+vl1+" vl2="+vl2+" vl3="+vl3);
		ArrayList<Short> pindices = new ArrayList<Short>();
		 _pindicesnew2=null;
		int p1,p2,p3,p4,p5,p6,p7,p8;
		//boolean t_intensity_flag=true;
		
	for(int i=0 ;i<(_ri.length-((vl2+1)*(vl3+1))-t_intensity.size());i++)
	//for(int i=0 ;i<(_ri.length-((vl2+1)*(vl3+1)));i++)
	{	
/*		t_intensity_flag=true;
		for(int i2=0 ;i2<t_intensity.size();i2++)
		{
			if (i==t_intensity.get(i2)) t_intensity_flag=false;
		}
		if (t_intensity_flag==true)
		{*/
		if ( ((i % (vl3+1)) < vl3 ) && (i% ((vl3+1)*(vl2+1)) < ((vl3+1)*vl2)) )
		     {   
		       //new algo
		       p1 =i;
		       p2 = i+(vl3+1);
		       p3= i+((vl2+1)*(vl3+1)+(vl3+1));
		       p4 = i+((vl2+1)*(vl3+1));
		       p5 = i+1;
		       p6 = i+((vl3+1)+1);
		       p7 = p3+1;
		       p8 = p4+1;
		       
		       pindices.add((short)p1);
		       pindices.add((short)p2);
		       pindices.add((short)p4);
		       pindices.add((short)p2);
		       pindices.add((short)p3);
		       pindices.add((short)p4);
		       
		       pindices.add((short)p1);
		       pindices.add((short)p2);
		       pindices.add((short)p5);
		       pindices.add((short)p2);
		       pindices.add((short)p5);
		       pindices.add((short)p6);
		       
		       pindices.add((short)p1);
		       pindices.add((short)p4);
		       pindices.add((short)p5);
		       pindices.add((short)p4);
		       pindices.add((short)p5);
		       pindices.add((short)p8);
		       
		       pindices.add((short)p2);
		       pindices.add((short)p3);
		       pindices.add((short)p6);
		       pindices.add((short)p3);
		       pindices.add((short)p6);
		       pindices.add((short)p7);
		       
		       
		       pindices.add((short)p3);
		       pindices.add((short)p4);
		       pindices.add((short)p8);
		       pindices.add((short)p3);
		       pindices.add((short)p7);
		       pindices.add((short)p8);
		       
		       pindices.add((short)p5);
		       pindices.add((short)p6);
		       pindices.add((short)p8);
		       pindices.add((short)p6);
		       pindices.add((short)p7);
		       pindices.add((short)p8);
		}
		//}
	}
	
	_pindicesnew2 = toPrimitiveShortArray(pindices);
		
	
		//end of indices
	
		//copy_xmax=copy_xmax-dec_value;
		pointcube1.set_pvertices(_rv);
		pointcube1.set_pcolors(_rc);
		pointcube1.set_pindices(_pindicesnew2);
		pointcube1.set_noVertices(_rv.length/3);
		_VortexRenderer.setPointcube(pointcube1);	
		slice_flag+=1;
		
		 for(int i=0;i<t_intensity.size();i++)
		  {
			Log.v("trial intensity"," t_intensity="+  t_intensity.get(i));
		  }
	}
	}
	
	//conversion ofstring array to float array	
	public float[] convertStringArraytoFloatArray(String[] _new) throws Exception 
	{
		if (_new != null)
		{
			float floatarray[] = new float[_new.length];
			for (int i = 0; i < _new.length; i++) {
				floatarray[i] = Float.parseFloat(_new[i]);
			}
			return floatarray;
		}
		return null;
	}
	//Convertion from a object to premitive array
	private float[] toPrimitiveArray(final ArrayList<Float> vertexList) {
	    final float[] primitives = new float[vertexList.size()];
	    int index = 0;
	    for (Float object : vertexList) {
	        primitives[index++] = object;
	    }
	    return primitives;
	}
	
	//Called when loading a file
	private void loadFileList(){
		try{
			mPath.mkdirs();
		}
		catch(SecurityException e){
			Log.e("log", "unable to write on the sd card " + e.toString());
		}
		if(mPath.exists()){
			FilenameFilter filter = new FilenameFilter(){
				public boolean accept(File dir, String filename){
					File sel = new File(dir, filename);
					return filename.contains(FTYPE) || sel.isDirectory();
				}
			};
			mFileList = mPath.list(filter);
		}
		else{
			mFileList= new String[0];
		}
	}
	
	 private short[] toPrimitiveShortArray(final ArrayList<Short> vertexList)
	  {
	    final short[] primitives = new short[vertexList.size()];
	    int index = 0;
	    for (Short object : vertexList) {
	        primitives[index++] = object;
	    }
	    return primitives;
	    }
}


